package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1。连接交换机
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        //2.创建队列/创建交换机/绑定
        Channel c = f.newConnection().createChannel();
        //队列声明
        String s = UUID.randomUUID().toString();
        c.queueDeclare(s, false, true, true, null);//随机名称，不持久，排他，自动删除，参数空
        //交换机声明
        c.exchangeDeclare(s, "fanout");
        //指定队列发送消息
        c.queueBind(s, "logs", "");//交换机和队列进行绑定
        //3。回调处理消息队列:DeliverCallback+
        DeliverCallback deliver = new DeliverCallback() {
            @Override
            public void handle(String tag, Delivery message) throws IOException { //参数：回执信息，接收信息
                String s = new String(message.getBody());
                System.out.println("收到：" + s);
            }
        };
        CancelCallback cancel = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        //4.第一个参数：s/"fanout"
        c.basicConsume("fanout", false, deliver, cancel);//消费：
    }
}
